#include "ServerInfo.h"
#include "ui_ServerInfo.h"

#include <QNetworkInterface>

ServerInfo::ServerInfo(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ServerInfo)
{
    ui->setupUi(this);
}

ServerInfo::~ServerInfo(){
    delete ui;
}

void ServerInfo::setServerName(const QString &name){
    ui->name->setText(name);
}

void ServerInfo::setServerPort(ushort port){
    ui->port->setText(QString::number(port));
}

void ServerInfo::setPlayerNum(int num){
    ui->playerNum->setText(QString::number(num));
}

void ServerInfo::showEvent(QShowEvent *){
    QString item("%1(%2)");
    foreach(const QNetworkInterface &interface, QNetworkInterface::allInterfaces()){
        if(!interface.isValid()
                || (interface.flags() & QNetworkInterface::IsLoopBack) == QNetworkInterface::IsLoopBack
                || (interface.flags() & QNetworkInterface::IsUp) != QNetworkInterface::IsUp){
            continue;
        }

        foreach(const QNetworkAddressEntry &addr, interface.addressEntries()){
            if(addr.ip().protocol() == QAbstractSocket::IPv4Protocol){
                ui->ipv4Address->addItem(item.arg(interface.humanReadableName()).arg(addr.ip().toString()));
            //}else if(addr.ip().protocol() == QAbstractSocket::IPv6Protocol){
            //    ui->ipv6Address->addItem(item.arg(interface.humanReadableName()).arg(addr.ip().toString()));
            }
        }
    }
}
